<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 商城
 * Class ShopController
 * @package App\Http\Controllers\Admin
 */
class ShopController  extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function index()
    {
        return view('admin.shop.goods');
    }

    public function goodsList()
    {
        $list = DB::table('shop_goods')
            ->orderBy('id', 'desc')
            ->get();
        return successResp($list);
    }

    public function editGoods($id)
    {
        $detail = DB::table('shop_goods')->find($id);
        return view('admin.shop.goodsEdit', ['detail' => $detail]);
    }

    public function updateGoods(Request $request, $id)
    {
        //验证表单
        $this->validate($request, [
            'title' => ['required','string', 'max:16'],
            'image' => ['required','string'],
            'integral' => ['required','int'],
            'store' => ['required','int'],
            'sort' => ['required','int'],
        ]);
        $data = [
            'title' => $request->title,
            'image' => $request->image,
            'integral' => $request->integral,
            'store' => $request->store,
            'sort' => $request->sort,
        ];
        if($id != 0){
            // 更新
            DB::table('shop_goods')
                ->where('id', $id)
                ->update($data);
        }else{
            // 创建
            DB::table('shop_goods')
                ->insert($data);
        }
        return successResp('');
    }

    public function destroy(Request $request)
    {
        $id = $request->id;
        if (!$id) {
            return errorResp('id empty');
        }
        DB::table('shop_goods')
            ->where('id',$id)
            ->delete();
        return successResp('');
    }

    public function orderPage()
    {
        return view('admin.shop.orders');
    }

    /**
     * 订单列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function orderList()
    {
        $list = DB::table('shop_order')
            ->where(function ($query) {
                if (@$_GET['union_id']) {
                    $query->where('union_id', @$_GET['union_id']);
                }
                if (@$_GET['status']) {
                    $query->where('status', @$_GET['status']);
                }
            })
            ->leftJoin('users','users.id', '=', 'shop_order.member_id')
            ->select(['shop_order.*', 'users.unionid'])
            ->orderBy('shop_order.id', 'desc')
            ->paginate(10);
        return successResp($list);
    }

    /**
     * 订单发货
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function posterOrder(Request $request)
    {
        //验证表单
        $this->validate($request, [
            'id' => ['required','int'],
            'post_no' => ['required','string'],
        ]);
        $log = DB::table('shop_order')->find($request->id);
        if (!$log) {
            return errorResp('订单错误');
        }
        if ($log->status == 3) {
            return errorResp('订单已签收');
        }
        DB::table('shop_order')->where('id', $request->id)->update(['status'=>2, 'post_no'=>$request->post_no]);
        return successResp('');
    }

    public function getAddress($id)
    {
        $log = DB::table('shop_member_address')
            ->find($id);
        return successResp($log);
    }
}
